package com.zhanbo.zhanbowymusic.main.featured;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.PlayListItemBean;
import com.mwx.baselib.bean.PlayListSongBean;
import com.mwx.baselib.bean.RanKingSongListBean;
import com.mwx.baselib.bean.RankingListBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.DateUtils;
import com.mwx.baselib.utils.GlideUtils;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentFeaturedPlaylistDetailsBinding;
import com.zhanbo.zhanbowymusic.main.featured.adapter.RanKingDetailsListAdapter;
import com.zhanbo.zhanbowymusic.main.mine.create.adapter.CreatePlaylistDetailsListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 排行榜详情
 */
public class RanKingListDetailsFragment extends BaseFragment<FragmentFeaturedPlaylistDetailsBinding> {
    private RanKingDetailsListAdapter adapter;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_featured_playlist_details;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            RankingListBean.ItemsDTO itemsDTO= (RankingListBean.ItemsDTO) getArguments().getSerializable("data");
            binding.tvTitle.setText(itemsDTO.getName()+"");
            binding.likeTitleText.setText(itemsDTO.getName()+"");
            binding.tvUpdateTime.setText(DateUtils.ISO8601Format(itemsDTO.getUpdateTime()+""));
//            binding.tvIntroduce
            GlideUtils.loadImage(_mActivity, itemsDTO.getImage(), binding.ivHead);

            NetworkManager.getInstance().getApiService().rankingSongList(itemsDTO.getId())
                    .subscribeOn(Schedulers.io())
                    .compose(this.bindToLifecycle())
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<List<RanKingSongListBean>>>(_mActivity) {
                        @Override
                        public void onSuccess(BaseResponses<List<RanKingSongListBean>> listBaseResponses) {
                            binding.tvListNum.setText(listBaseResponses.getData().size()+"");
                            adapter.getItems().addAll(listBaseResponses.getData());
                            binding.rvFeaturedListDetails.setAdapter(adapter);
                            binding.ivPlayAll.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                   List<SongBean>  songBeans = new ArrayList<>();
                                    for (RanKingSongListBean datum : listBaseResponses.getData()) {
                                        songBeans.add(datum.getSong());
                                    }
                                    //播放全部歌曲
                                    MusicController.getInstance().updateAndPlaySheetData(songBeans);
                                }
                            });
                        }
                    });
        }
    }

    @Override
    protected void initView() {
        binding.rvFeaturedListDetails.setLayoutManager(new LinearLayoutManager(_mActivity,LinearLayoutManager.VERTICAL, false));
         adapter = new RanKingDetailsListAdapter(_mActivity);

    }

    @Override
    protected void initListener() {
        binding.iconBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });


    }

    public static RanKingListDetailsFragment newInstance(RankingListBean.ItemsDTO itemsDTO) {

        Bundle args = new Bundle();
        args.putSerializable("data",itemsDTO);
        RanKingListDetailsFragment fragment = new RanKingListDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onNewBundle(Bundle args) {
        super.onNewBundle(args);
    }
}
